package com.haiyou.dc.thread.test3;

/**
 * InheritableThreadLocal：根据上下文环境，将主线程的数据拷贝一份给子线程，子修改不能影响主线程
 */
public class TestThreadLocal04 {


    private static ThreadLocal<Integer> threadLocal = new InheritableThreadLocal<>();

    public static void main(String[] args) {

        threadLocal.set(2);
        System.out.println(Thread.currentThread().getName() + "------>" + threadLocal.get());

        new Thread(new MyRun()).start();
        new Thread(new MyRun()).start();
        System.out.println(Thread.currentThread().getName() + "------>" + threadLocal.get());

    }

    static class MyRun implements Runnable {


        @Override
        public void run() {
            threadLocal.set((int) (Math.random() * 99));
            System.out.println(Thread.currentThread().getName() + "------>" + threadLocal.get());

        }
    }

}
